﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using huaguwang.WebControls;

namespace huaguwang.hgmanage.Power
{
    public partial class ButtonSet1 : PageBaseAdmin
    {
        private readonly D_T_Button_Exp _dal = new D_T_Button_Exp();
        protected void Page_Load(object sender, EventArgs e)
        {
            Pager.PageSize = 10;
            //用来初始化用户的按钮,执行此语句的时候,but并没有具体产生
            WebUserToolBar1.MainInit(
               UserConst.Exp_GetUser,
               UserConst.Exp_GetButtonList,
               (i, but) =>
               {
                   switch (i)
                   {
                       case 1:
                           but.Attributes.Add("onclick", "return GetUpdateCheckDefault();");
                           break;
                       case 2:
                           but.Attributes.Add("onclick", "return GetDelCheckDefault();");
                           break;
                       default:
                           break;
                   }

               },
              UserConst.Exp_ToolBarErr,
               new huaguwang.WebControls.WebUserToolBar.SelectItem()
               {
                   FuncGetText = () => TextBox1.Text.Trim(),
                   FuncSetText = v => { TextBox1.Text = v; },
                   Name = TextBox1.ID
               },
                new huaguwang.WebControls.WebUserToolBar.SelectItem()
                {
                    FuncGetText = () => TextBox2.Text.Trim(),
                    FuncSetText = v => { TextBox2.Text = v; },
                    Name = TextBox2.ID
                }
            );
            if (!IsPostBack)
            {
                //分页查询
                SetData(0);
            }

        }

        #region 基本的带有查询的事件组合
        /// <summary>
        /// 分页控件的页面事件
        /// </summary>
        protected void Pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            SetData((e.NewPageIndex - 1) * Pager.PageSize);
        }
        /// <summary>
        /// 查询的实现方法
        /// </summary>
        /// <param name="begin"></param>
        public void SetData(int begin)
        {
            AdminPublic.Try(() =>
                {
                    var txtPName = this.WebUserToolBar1.MainGetQuery(this.TextBox1.ID);
                    var txtName = this.WebUserToolBar1.MainGetQuery(this.TextBox2.ID);
                    var count = 0;

                    this.Repeater1.DataSource = _dal.GetListPage(begin, Pager.PageSize, txtPName, txtName, out count);
                    this.Repeater1.DataBind();
                    Pager.RecordCount = count;
                });

        }
        /// <summary>
        /// 查询按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button3(object sender, EventArgs e)
        {
            WebUserToolBar1.MainSetQuery();
            SetData(0);
        }
        /// <summary>
        /// 增加按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            var sKey = this.WebUserToolBar1.MainSetSKey();
            Response.Redirect("ButtonSet_E.aspx?" + sKey, false);
        }


        /// <summary>
        /// 修改按钮的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var sKey = this.WebUserToolBar1.MainSetSKey();
            Response.Redirect("ButtonSet_E.aspx?ID=" + strID + "&" + sKey, false);
        }
        /// <summary>
        /// 删除按钮的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button2(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
               {
                   var strID = Request.Params["HiddenID"].Exp_Trim();
                   var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
                   if (intA == null || intA.Count <= 0)
                   {
                       this.Exp_PageAlter("没有找到可以删除的信息");
                       return;
                   }

                   int i = 0;
                   foreach (var item in intA)
                   {
                       i++;
                       //检测权限
                       var tempI = _dal.GetPowerCount(item);
                       if (tempI > 0)
                       {
                           this.Exp_PageAlter("当前第" + i + "条资料出现在有效权限组中,不能够直接删除");
                           return;
                       }
                   }

                   var refInt = _dal.UpdateFlag(intA);
                   if (refInt > 0)
                   {

                       /*add-upd-del-add-upd-del-add-upd-del-add-upd-del-add-upd-del-add-upd-del-add-upd-del-
                       //************************************************************************************
                       // 更改写日志功能
                       //  邹兴武    at    2010－12－2
                       //  日志功能已更改
                       //************************************************************************************
                       // add-upd-del-add-upd-del-add-upd-del-add-upd-del-add-upd-del-add-upd-del-add-upd-del-*/
                       //this.Exp_PageWriteLog("资料删除成功",
                       //    "删除成功,删除的id:{0},共{1}个,成功删除{2}个".Exp_Format(strID, intA.Count, refInt),
                       //    "删除按钮资料",
                       //    true);

                       this.Exp_PageWriteLog(Log_Type.delete, "按钮信息", intA);
                       SetData(0);
                       this.Exp_PageAlter("删除操作成功");
                       return;
                   }
                   else
                   {
                       this.Exp_PageAlter("删除操作失败");
                       return;
                   }
               });
            //this.Exp_PageAlter("获得了" + strID);
        }
        #endregion


    }
}
